package com.agricultural.equipment;

import com.agricultural.observer.Observer;
import com.agricultural.strategy.PathPlanningStrategy;
import com.agricultural.strategy.impl.DefaultPathStrategy;
import com.agricultural.state.EquipmentState;
import com.agricultural.state.impl.IdleState;

import java.util.HashMap;
import java.util.Map;

// 农机设备抽象类
public abstract class AgriculturalEquipment implements Observer {
    protected String id;                 // 设备ID
    protected String type;               // 设备类型
    protected double positionX;          // X坐标位置
    protected double positionY;          // Y坐标位置
    protected double fuelLevel;          // 燃油水平
    protected EquipmentState state;      // 设备状态
    protected PathPlanningStrategy pathPlanningStrategy; // 路径规划策略
    protected Map<String, Object> statusData; // 设备状态数据

    // 构造函数
    public AgriculturalEquipment(String id, String type) {
        this.id = id;
        this.type = type;
        this.positionX = 0.0;
        this.positionY = 0.0;
        this.fuelLevel = 100.0;
        this.state = new IdleState(this); // 初始状态为闲置
        this.pathPlanningStrategy = new DefaultPathStrategy(); // 默认路径规划策略
        this.statusData = new HashMap<>();
        initStatusData();
    }

    // 初始化状态数据
    private void initStatusData() {
        statusData.put("position", String.format("(%.2f, %.2f)", positionX, positionY));
        statusData.put("fuelLevel", fuelLevel);
        statusData.put("state", state.getStateName());
    }

    // 启动设备
    public void start() {
        state.start();
    }

    // 停止设备
    public void stop() {
        state.stop();
    }

    // 执行作业
    // 执行作业
    public void executeWork() {
        // 消耗燃油
        if (fuelLevel > 0) {
            fuelLevel -= 5.0;
            updateStatusData("fuelLevel", fuelLevel);

            // 检查燃油是否不足
            if (fuelLevel < 20) {
                System.out.println("[" + type + "-" + id + "] 燃油不足，即将停止作业");
                stop(); // 自动停止作业
            }
        } else {
            System.out.println("[" + type + "-" + id + "] 燃油耗尽，停止作业");
            stop();
        }
    }

    // 规划路径
    public void planPath(Map<String, Object> environmentData) {
        System.out.println("[" + type + "-" + id + "] 正在规划路径...");
        pathPlanningStrategy.planPath(this, environmentData);
    }

    // 设置路径规划策略
    public void setPathPlanningStrategy(PathPlanningStrategy strategy) {
        System.out.println("[" + type + "-" + id + "] 切换路径规划策略为: " + strategy.getStrategyName());
        this.pathPlanningStrategy = strategy;
    }

    // 更新设备状态
    public void updateState(EquipmentState state) {
        this.state = state;
        updateStatusData("state", state.getStateName());
        System.out.println("[" + type + "-" + id + "] 状态更新为: " + state.getStateName());
    }

    // 更新状态数据
    public void updateStatusData(String key, Object value) {
        statusData.put(key, value);
        if ("position".equals(key)) {
            // 如果是位置更新，解析坐标值
            String[] pos = value.toString().substring(1, value.toString().length() - 1).split(",");
            this.positionX = Double.parseDouble(pos[0].trim());
            this.positionY = Double.parseDouble(pos[1].trim());
        }
    }

    // 获取设备状态信息
    public Map<String, Object> getStatus() {
        return statusData;
    }

    // 获取设备ID
    public String getId() {
        return id;
    }

    // 获取设备类型
    public String getType() {
        return type;
    }

    // 获取当前状态
    public EquipmentState getState() {
        return state;
    }

    // 显示设备信息
    public void displayInfo() {
        System.out.println("设备类型: " + type);
        System.out.println("设备ID: " + id);
        System.out.println("当前位置: (" + positionX + ", " + positionY + ")");
        System.out.println("燃油水平: " + fuelLevel + "%");
        System.out.println("当前状态: " + state.getStateName());
        System.out.println("-------------------------");
    }

    // 实现Observer接口的update方法
    @Override
    public void update(Map<String, Object> data) {
        System.out.println("[" + type + "-" + id + "] 收到环境数据更新通知");

        // 根据环境数据调整设备状态或行为
        if (data.containsKey("soilMoisture")) {
            double soilMoisture = (double) data.get("soilMoisture");
            System.out.println("土壤湿度: " + soilMoisture + "%");

            // 根据土壤湿度调整路径规划策略
            if (soilMoisture > 70.0) {
                // 湿度过高，切换到湿土路径策略
                // 注意：这里需要确保WetSoilPathStrategy类已实现
                // setPathPlanningStrategy(new WetSoilPathStrategy());
                System.out.println("湿度过高，需要调整作业策略");
            }
        }

        if (data.containsKey("obstacle")) {
            double obstacleDistance = (double) data.get("obstacle");
            System.out.println("检测到障碍物，距离: " + obstacleDistance + "米");

            // 触发安全处理机制
            // safetyHandlerChain.handleSafetyEvent(this, "obstacle", obstacleDistance);
        }
    }
}